<template>
	<view class='main-container'>
		<!--头部 搜索框开始-->
		<view class='search'>
			<uni-icon type="arrowleft" @click="back" size="26" color="#ffffff" style="margin-bottom: 20upx;margin-left: 10upx;"></uni-icon>
			<view class='inp'>
				<image src='../../static/images/search1.png'></image>
				<input placeholder="搜索商品" auto-focus v-model="searchValue" maxlength="15" />
				<image src='../../static/images/close1.png' v-if="searchValue.length!=0" @tap='clear'></image>
			</view>
			<text @tap='search'>搜索</text>
		</view>
		<!--推荐搜索块开始-->
		<text class='his' v-if="isDis" :style="top">热门搜索</text>
		<view class='tuijian' v-if="isDis">
			<view v-for="(item,index) in tuijians" :key="index" @tap='tuijianSearch(item.title)'>
				{{item.title}}
			</view>
		</view>
		<!-- 搜索历史开始 -->
		<view class="goodslist" :style="top">
			<view class="goods" v-for="(item,index) in goods" :key="index" @tap="go_detail(index)">
				<image :src="getRealPath(item.goodsImgUrl)" mode=""></image>
				<view class="name">{{item.goodsName}}</view>
				<view class="bottom">
					<view class="price">￥{{item.goodsPrice}}</view>
					<view class="time">{{formatTime(item.createTime)}}</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import utils from "../../static/js/utils.js";
	import uniIcon from "@/components/uni-icon/uni-icon.vue"
	let top;
	// #ifdef APP-PLUS
	const device = uni.getSystemInfoSync();
    top = device.statusBarHeight + uni.upx2px(120);
	// #endif
	// #ifdef MP-WEIXIN
	 top = uni.upx2px(120) + 100;
	// #endif
	export default {
		components: {
			uniIcon
		},
		computed: {
			top() {
				return "margin-top:" + top + "px";
			}
		},
		data() {
			return {
				goods: [],
				isDis: true,
				searchValue: '',
				tuijians: [{
						id: 1,
						title: '电影票'
					},
					{
						id: 2,
						title: '口红'
					},
					{
						id: 3,
						title: '考研资料'
					},
					{
						id: 4,
						title: '小米9'
					},
					{
						id: 5,
						title: '阿迪'
					}
				]
			}
		},
		methods: {
			go_detail(index){
				uni.navigateTo({
					url: 'goodsdetail?good='+JSON.stringify(this.goods[index])
				});
			},
			formatTime(timestamp) {
				var da = new Date(timestamp);
				return (da.getMonth() + 1) + "-" + da.getDate();
			},
			getRealPath(path) {
				return utils.baseImgUrl + path;
			},
			tuijianSearch(value) {
				this.searchValue = value;
				this.doSearch(value);
			},
			search() {
				this.doSearch(this.searchValue);
			},
			clear() {
				this.searchValue = '';
				this.isDis = true;
				this.goods=[];
			},
			back() {
				uni.navigateBack({
					delta: 1
				});
			},
			doSearch(value) {
				if (value.length == 0) {
					return;
				}
				utils.loading("搜索中...");
				this.isDis = false;
				let that = this;
				utils.get("market/search", {
					searchValue: value
				}, function(data) {
					that.goods = data;
				});
			}

		},
		onLoad() {

		}
	}
</script>

<style>
	page {
		background-color: #f6f6f6;
	}

	.main-container {
		display: flex;
		width: 100%;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.search {
		width: 100%;
		background-color: #2E77ED;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		position: fixed;
		top: 0;
		z-index: 999;
		/* #ifdef MP-WEIXIN */
		padding-top: 90px;
		/* #endif */
		/* #ifdef APP-PLUS */
		padding-top: var(--status-bar-height);
		/* #endif */
		background: linear-gradient(to left, #fa4dbe 0, #fbaa58 100%);

	}

	.inp {
		width: 590upx;
		height: 60upx;
		display: flex;
		flex-direction: row;
		background-color: white;
		align-items: center;
		margin-left: 20upx;
		border-radius: 10upx;
		font-size: 26upx;
		margin-bottom: 20upx;

	}

	.inp image {
		width: 48upx;
		height: 48upx;
	}

	.inp input {
		width: 554upx;
	}

	.search text {
		font-size: 28upx;
		margin-left: 10upx;
		color: white;
		height: 60upx;
	}

	.tuijian {
		width: 100%;
		height: 120upx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
	}

	.tuijian view {
		height: 45upx;
		font-size: 24upx;
		background-color: white;
		padding: 10upx 14upx;
		text-align: center;
		line-height: 45upx;
		margin-left: 25upx;
	}


	.his {
		margin-left: 15upx;
		font-size: 24upx;
		color: #666;
		margin-bottom: 20upx;
		margin-top: 20upx;
	}

	.goodslist {
		width: 92%;
		padding: 0 4% 3vw 4%;
		display: -webkit-box;
		display: -webkit-flex;
		display: -ms-flexbox;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		margin-top: 10upx;
		margin-bottom: 30upx;

	}

	.goods {
		width: 48%;
		border-radius: 20upx;
		background-color: #fff;
		margin: 0 0 15upx 0;
		box-shadow: 0upx 5upx 25upx rgba(0, 0, 0, 0.1);
		margin-top: 14upx;
	}

	.goods image {
		width: 100%;
		border-radius: 20upx 20upx 0 0;
		height: 166px;

	}

	.name {
		width: 92%;
		padding: 10upx 4%;
		text-align: justify;
		overflow: hidden;
		font-size: 30upx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.bottom {
		display: flex;
		justify-content: space-between;
		align-items: flex-end;
		width: 92%;
		padding: 10upx 4% 10upx 4%;


	}

	.price {
		color: #e65339;
		font-size: 30upx;
		font-weight: 600;


	}

	.time {
		color: #807c87;
		font-size: 24upx;


	}
</style>
